/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.core
{
	import funit.core.attributes.DescriptionAttribute;
	import funit.core.attributes.ExpectedErrorAttribute;
	import funit.core.attributes.ExplicitAttribute;
	import funit.core.attributes.IgnoreAttribute;
	import funit.core.attributes.SetUpAttribute;
	import funit.core.attributes.SuiteAttribute;
	import funit.core.attributes.TearDownAttribute;
	import funit.core.attributes.TestAttribute;
	import funit.core.attributes.TestFixtureAttribute;
	import funit.core.attributes.TestFixtureSetUpAttribute;
	import funit.core.attributes.TestFixtureTearDownAttribute;
	
	import mx.utils.StringUtil;
	
	import sv.reflection.Attribute;
	import sv.reflection.AttributeFactory;
	import sv.reflection.MemberFlags;
	import sv.reflection.MethodInfo;
	import sv.reflection.Type;
	import sv.utils.AttributeUtil;
	import sv.utils.ClassUtil;
	
	public class FUnitFramework
	{
		private static var initialized:Boolean = initialize();
		
		/**
		 * @return
		 */
		private static function initialize() : Boolean
		{
			AttributeFactory.registerAttribute("Test", TestAttribute);
			AttributeFactory.registerAttribute("Suite", SuiteAttribute);
			AttributeFactory.registerAttribute("TestFixture", TestFixtureAttribute);
			
			AttributeFactory.registerAttribute("Ignore", IgnoreAttribute);
			AttributeFactory.registerAttribute("Explicit", ExplicitAttribute);
			AttributeFactory.registerAttribute("Description", DescriptionAttribute);
			AttributeFactory.registerAttribute("ExpectedError", ExpectedErrorAttribute);
			
			AttributeFactory.registerAttribute("SetUp", SetUpAttribute);
			AttributeFactory.registerAttribute("TearDown", TearDownAttribute);
			AttributeFactory.registerAttribute("TestFixtureSetUp", TestFixtureSetUpAttribute);
			AttributeFactory.registerAttribute("TestFixtureTearDown", TestFixtureTearDownAttribute);
			
			return true;
		}
		
		//-------------------------------------------------------
		// SetUp and TearDown Validation Methods
		//-------------------------------------------------------
		
		/**
		 * @param method
		 * @return
		 */
		public static function isSetUpMethod( method:MethodInfo ) : Boolean
		{
			return AttributeUtil.hasAttribute(method, SetUpAttribute, false);
		}
		
		/**
		 * @param method
		 * @return
		 */
		public static function isTearDownMethod( method:MethodInfo ) : Boolean
		{
			return AttributeUtil.hasAttribute(method, TearDownAttribute, false);
		}
		
		/**
		 * @param method
		 * @return
		 */
		public static function isFixtureSetUpMethod( method:MethodInfo ) : Boolean
		{
			return AttributeUtil.hasAttribute(method, TestFixtureSetUpAttribute, false);
		}
		
		/**
		 * @param method
		 * @return
		 */
		public static function isFixtureTearDownMethod( method:MethodInfo ) : Boolean
		{
			return AttributeUtil.hasAttribute(method, TestFixtureTearDownAttribute, false);
		}
		
		//-------------------------------------------------------
		// SetUp and TearDown Locator Methods
		//-------------------------------------------------------
		
		/**
		 * @param fixtureType
		 * @return
		 */
		public static function getSetUpMethod( fixtureType:Type ) : MethodInfo
		{
			return	AttributeUtil.getMemberWithAttribute(
						fixtureType,
						SetUpAttribute,
						MemberFlags.Method | MemberFlags.Instance,
						true
					) as MethodInfo;
		}
		
		/**
		 * @param fixtureType
		 * @return
		 */
		public static function getTearDownMethod( fixtureType:Type ) : MethodInfo
		{
			return	AttributeUtil.getMemberWithAttribute(
						fixtureType,
						TearDownAttribute,
						MemberFlags.Method | MemberFlags.Instance,
						true
					) as MethodInfo;
		}
		
		/**
		 * @param fixtureType
		 * @return
		 */
		public static function getFixtureSetUpMethod( fixtureType:Type ) : MethodInfo
		{
			return	AttributeUtil.getMemberWithAttribute(
						fixtureType,
						TestFixtureSetUpAttribute,
						MemberFlags.Method | MemberFlags.Instance,
						true
					) as MethodInfo;
		}
		
		/**
		 * @param fixtureType
		 * @return
		 */
		public static function getFixtureTearDownMethod( fixtureType:Type ) : MethodInfo
		{
			return	AttributeUtil.getMemberWithAttribute(
						fixtureType,
						TestFixtureTearDownAttribute,
						MemberFlags.Method | MemberFlags.Instance,
						true
					) as MethodInfo;
		}
		
		//-------------------------------------------------------
		// Attribute Setter Methods
		//-------------------------------------------------------
		
		/**
		 * @param attributes
		 * @param test
		 */
		public static function applyCommonAttributes( attributes:Array, test:ITest ) : void
		{
			for each ( var attribute:Attribute in attributes )
			{
				var isValid:Boolean = test.runState != RunState.NotRunnable;
				
				if (attribute is TestAttribute)
				{
					if (test.description == null)
					{
						test.description = TestAttribute(attribute).description;
					}
				}
				else if (attribute is TestFixtureAttribute)
				{
					if (test.description == null)
					{
						test.description = TestFixtureAttribute(attribute).description;
					}
				}
				else if (attribute is DescriptionAttribute)
				{
					test.description = DescriptionAttribute(attribute).description;
				}
				else if (attribute is ExplicitAttribute)
				{
					if (isValid)
					{
						test.runState = RunState.Explicit;
						test.ignoreReason = ExplicitAttribute(attribute).reason;
					}
				}
				else if (attribute is IgnoreAttribute)
				{
					if (isValid)
					{
						test.runState = RunState.Ignored;
						test.ignoreReason = IgnoreAttribute(attribute).reason;
					}
				}
			}
		}
		
		/**
		 * @param method
		 * @param testMethod
		 */
		public static function applyExpectedErrorAttribute( method:MethodInfo, testMethod:TestMethod ) : void
		{
			var attribute:ExpectedErrorAttribute =
				AttributeUtil.getAttribute(method, ExpectedErrorAttribute, false) as ExpectedErrorAttribute;
			
			if (attribute != null)
			{
				testMethod.errorExpected = true;
				
				testMethod.expectedErrorName = attribute.errorName;
				testMethod.expectedErrorType = attribute.errorType;
				testMethod.expectedMessage = attribute.expectedMessage;
				
				if (!attribute.handler)
				{
					var interfaceType:Type =
						testMethod.fixtureType.getInterface(
							ClassUtil.getQualifiedClassName(IExpectError)
						);
					
					if (interfaceType != null)
					{
						testMethod.errorHandler =
							testMethod.fixtureType.getMethod("handleError");
					}
				}
				else
				{
					var handler:MethodInfo =
						testMethod.fixtureType.getMethod(attribute.handler);
					
					if ( handler != null )
					{
						testMethod.errorHandler = handler;
					}
					else
					{
						testMethod.runState = RunState.NotRunnable;
						testMethod.ignoreReason =
							StringUtil.substitute(
								"The specified error handler {0} was not found",
								attribute.handler
							);
					}
				}
			}
		}
		
	}
	
}